//
//  LoginAndRegisterController.m
//  FD
//
//  Created by Yasin on 2018/10/12.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "LoginAndRegisterController.h"
#import "FDTabBarController.h"
#import "LoginViewController.h"
#import "NewRegisterViewController.h" //注册
#import "EvaluateController.h" //11-13 测试
#import "FDNavigationViewController.h" //1-17

#import "FDUserTool.h" //自动登录
#import "FDPushWebSocketTool.h" //推送socket

@interface LoginAndRegisterController ()
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UIButton *accountLoginBtn;

@property (weak, nonatomic) IBOutlet UIButton *skipBtn;

@end

@implementation LoginAndRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.registerBtn.layer.borderWidth = 1.0;
    self.registerBtn.layer.borderColor = [UIColor redColor].CGColor;
    
//    //国际化测试
//    UILabel *testInternationalLb = [UILabel new];
//    testInternationalLb.frame = CGRectMake(10, 300, 200, 30);
//    testInternationalLb.backgroundColor = [UIColor lightGrayColor];
//    NSString *testStr = NSLocalizedString(Test, @"11-23测试");
//    testInternationalLb.text = testStr;
//    [self.view addSubview:testInternationalLb];
    
    
    //国际化
    [self internationalization];
}
- (void)internationalization {
    NSString *registerStr = NSLocalizedString(Register, nil);
    NSString *accountLogin = NSLocalizedString(AccountLogin, nil);
    [self.registerBtn setTitle:registerStr forState:(UIControlStateNormal)];
    [self.accountLoginBtn setTitle:accountLogin forState:(UIControlStateNormal)];
    
    [self.registerBtn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)]; //红
    [self.accountLoginBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)]; //白
    [self.skipBtn setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
    
    NSString *skipStr = NSLocalizedString(Skip, nil);
    
    //添加底部下划线、背景颜色
    UIColor *xxgrayColor = rgba(153, 153, 153, 1.0);

    //下划线
    NSMutableDictionary *bottomLineAttDic = @{}.mutableCopy;
    bottomLineAttDic[NSUnderlineStyleAttributeName] = @(NSUnderlineStyleSingle);
    bottomLineAttDic[NSForegroundColorAttributeName] = xxgrayColor;
    
    NSMutableAttributedString *skipAttM = [[NSMutableAttributedString alloc] initWithString:skipStr attributes:bottomLineAttDic];
    [self.skipBtn setAttributedTitle:skipAttM forState:(UIControlStateNormal)];
    
    [self.skipBtn setTitleColor:xxgrayColor forState:(UIControlStateNormal)];
    
    //xib 这种修改方式无效
    //    self.registerBtn.titleLabel.text = registerStr;
    //    self.accountLoginBtn.titleLabel.text = accountLogin;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES]; //隐藏导航条
    
    //自动登录
//    sleep(1.5);
    [self autoLogin];
}



#pragma mark - 点击事件
//登陆
- (IBAction)loginAction:(UIButton *)sender {
    
    [self skipToLoginVC];
}

//注册
- (IBAction)registerBtnAction:(UIButton *)sender {
    
//    RegisterController *registerVC = [[RegisterController alloc] initWithNibName:@"RegisterController" bundle:nil];
//
//    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:registerVC];
//
//    [self presentViewController:navVC animated:YES completion:nil];
    
    NewRegisterViewController *registerVC = [[NewRegisterViewController alloc] init];
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:registerVC];
    [self presentViewController:navVC animated:YES completion:nil];
}

//跳过
- (IBAction)skipAction:(UIButton *)sender {
    
//    EvaluateController *evaluteVC = [[EvaluateController alloc] init];
//    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:evaluteVC];
//    [self presentViewController:navVC animated:YES completion:nil];
    [self skipToFDTabbarVC];
}



- (void)skipToFDTabbarVC {
    
    [self xxReconnectPushSocketWhenAutoLogin]; //2019-1-19 重行建立socket连接
    
    FDTabBarController *fdtabbarVC = [FDTabBarController shareFDTabarVC];
    [UIApplication sharedApplication].keyWindow.rootViewController = fdtabbarVC;
}

- (void)skipToLoginVC {
    
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    
    FDNavigationViewController *navVC = [[FDNavigationViewController alloc] initWithRootViewController:loginVC];
    [self presentViewController:navVC animated:YES completion:nil];
}


- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}


#pragma mark - 自动登录

- (void)autoLogin {
    
    //如果没有退出登录,每次启动都自动登录
    if ([FDUserTool fd_account] && [FDUserTool fd_password]) {
        NSDictionary *parDic = @{@"name":[FDUserTool fd_account],@"password":[FDUserTool fd_password]};
        
        [MBProgressHUD fd_showMessage:@"" toView:self.view]; //菊花
        
        [FDHttpAPi requestLoginWithParDic:parDic successBlock:^(id responseObjc) {
            NSString *event = responseObjc[@"event"];
            if ([event isEqualToString:SUCCESS]) {
                
                //保存账号
                [self skipToFDTabbarVC]; //请求成功跳转界面
            }else {
                [JRToast showWithText:responseObjc[describe]];//请求失败
            }
            
            [MBProgressHUD fd_hideHUDFromView:self.view]; //移除菊花
        } failureBlock:^(NSError *error) {
            FDLog(@"%@",[error localizedDescription]);
            [MBProgressHUD fd_hideHUDFromView:self.view];
        }];
    }
}

#pragma mark  - 1-19、用户登录-》退出APP-》再次打开APP，自动登录后，没有与消息推送websocket建立连接
- (void)xxReconnectPushSocketWhenAutoLogin {
    
    //2019-1-19如果判断断开了socket连接 NO, 自动登录的时候需要重新建立连接
    if (![FDPushWebSocketTool defaultFdWebPushSockcet].isFDPushWebSocketConnect) {
        [[FDPushWebSocketTool defaultFdWebPushSockcet] setupAndOpenPushWebSocket];
    }
}

@end
